﻿
Imports std

Namespace delegateClient
    Public Module _delegateClient
        Private _talk As tcpTalk = Nothing

        Private Function talk() As tcpTalk
            assert(Not _talk Is Nothing)
            Return _talk
        End Function

        Public Function jobid() As String
            Static _jobid As String = Nothing
            If _jobid Is Nothing Then
                _jobid = Environment.GetEnvironmentVariable(environment_variables.localrunJobId)
                assert(Not isemptystring(_jobid))
            End If
            Return _jobid
        End Function

        Public Function updateStatus(ByVal p As percentage_t) As Boolean
            Dim jobid As String = Nothing
            jobid = Environment.GetEnvironmentVariable(environment_variables.localrunJobId)
            If isemptystring(jobid) Then
                Return False
            End If

            talk().sendData.clear()
            nettransDataInserter.keyvaluepair(talk().sendData, _
                                              protocols.action, protocols.actions.updateStatus, _
                                              protocols.jobid, jobid, _
                                              protocols.percentage, Convert.ToString(p))
            If Not talk().talk() OrElse talk().receiveData() Is Nothing Then
                raiseError("cannot talk with localrun server.", errorHandle.errorType.warning)
                Return False
            ElseIf talk().receiveData().size() = 0 Then
                raiseError("localrun server does no send response.", errorHandle.errorType.warning)
                Return False
            Else
                Dim ps As vector(Of pair(Of String, String)) = Nothing
                If Not nettransDataParser.keyvaluepair(talk().receiveData(), ps) OrElse ps Is Nothing Then
                    raiseError("cannot parse keyvalue pairs from receivedata.", errorHandle.errorType.warning)
                    Return False
                Else
                    For i As Int64 = 0 To ps.size() - 1
                        If matchProtocol(ps(i), protocols.result) Then
                            If matchResult(ps(i), protocols.results.success) Then
                                Return True
                            Else
                                Return False
                            End If
                        End If
                    Next

                    raiseError("localrun server does not response result protocol.", errorHandle.errorType.warning)
                    Return False
                End If
            End If
        End Function

        Sub New()
            _talk = New tcpTalk()
            _talk.host = network.localhost
            _talk.port = connection_const.port.localrun
            _talk.holdtime = connection_const.delegate_localrun.holdtime
            _talk.livetime = connection_const.delegate_localrun.livetime
        End Sub
    End Module
End Namespace
